package com.facebook.utils 
{
	import flash.utils.getTimer;

	import com.facebook.commands.FacebookCall;
	import com.facebook.data.FacebookData;
	import com.facebook.utils.connectionUtils.abstract.aConnectUtil;

	import flash.external.ExternalInterface;

	/**
	 * @author Ben Smith
	 * Thank you to ADOBE for their original creation of this class;
	 */
	public class FacebookPostUtils extends aConnectUtil 
	{

		
		protected static var externalInterfaceCalls : Object = {};
		protected static var hasPostCallback : Boolean = false;

		protected static var apiURL : String = "https://api.facebook.com/method/"

		
		
		public function FacebookPostUtils() 
		{
                      
                        
			if (hasPostCallback == false) {
				ExternalInterface.addCallback('handlePostCallback', handlePostConnectCallback);
				hasPostCallback = true;
			}
		}

		
		
		
		
		static public function registerCall(fbCall : FacebookCall) : void 
		{
            	
			var callId : String = String(getTimer())
			externalInterfaceCalls[callId] = fbCall;
		
			ExternalInterface.call("jsonp", apiURL + (fbCall.method).toString(),callId,'callBack_'+callId, fbCall.args.toString());
		}

		
		
		protected static function handlePostConnectCallback(result : Object,identifier : String) : void 
		{
			
			var call : FacebookCall = externalInterfaceCalls[identifier];
			if (result) {
				var fbData : FacebookData = new FacebookData();
				fbData.result = result as Object
				call.handleResult(fbData);
			} 
                        
			delete externalInterfaceCalls[identifier];
		}
	}
}
